function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/*
 * @Author: hc
 * @Date: 2024-03-06 15:45:18
 * @LastEditTime: 2024-03-12 14:22:14
 * @LastEditors: hc
 * @Description: 条件节点组件
 */
import { message } from "../../../../functionalComponent";
import { iup } from "../../../../util";
import { ROW_PERMISSION_RELATION } from "../../types";
import { RulesController } from "../rulesController";
import { jsx as _jsx } from "react/jsx-runtime";
var isDisabled = function isDisabled(props) {
  return props.disabled === true;
};
export var FilterRules = function FilterRules(props) {
  var component = props.component,
    _props$disabled = props.disabled,
    disabled = _props$disabled === void 0 ? false : _props$disabled,
    value = props.value,
    onChange = props.onChange;
  var _ref = !isDisabled(props) && props,
    _ref$maxLevel = _ref.maxLevel,
    maxLevel = _ref$maxLevel === void 0 ? 3 : _ref$maxLevel,
    _ref$maxSize = _ref.maxSize,
    maxSize = _ref$maxSize === void 0 ? 100 : _ref$maxSize,
    _ref$notEmpty = _ref.notEmpty,
    notEmpty = _ref$notEmpty === void 0 ? {
      data: true,
      message: '必须有一条数据'
    } : _ref$notEmpty,
    initValues = _ref.initValues;

  /**
   * 查找当前操作的节点
   */
  var findRelationNode = function findRelationNode(parentData, targetKey, needCurrent) {
    var _parentDataTemp$child;
    var parentDataTemp = parentData;
    if (parentDataTemp.key === targetKey) return parentDataTemp;
    if (!((_parentDataTemp$child = parentDataTemp.children) !== null && _parentDataTemp$child !== void 0 && _parentDataTemp$child.length)) return null;
    for (var i = 0; i < parentDataTemp.children.length; i++) {
      var current = parentDataTemp.children[i];
      if (current.key === targetKey) return needCurrent ? current : parentDataTemp;
      var node = findRelationNode(current, targetKey, needCurrent);
      if (node) return node;
    }
  };

  /**
   * 处理条件新增
   */
  var handleAddCondition = function handleAddCondition(keyObj) {
    var cloneData = iup.deepCopyEx(value);
    var appendNode = findRelationNode(cloneData, keyObj.key, keyObj.isOut);
    addCondition(appendNode, keyObj, initValues);
    onChange === null || onChange === void 0 || onChange(cloneData);
  };

  /**
   * 增加新的数据
   * - 判断是在当前节点下新增或者新生成一个条件节点
   */
  var addCondition = function addCondition(treeNode, keyObj, initRowValue) {
    var key = keyObj.key;
    if (keyObj.isOut) return treeNode.children.push(Object.assign({}, {
      rowValues: initRowValue
    }, {
      key: iup.uniqueId(),
      level: treeNode.level
    }));
    var children = treeNode === null || treeNode === void 0 ? void 0 : treeNode.children;
    if (!children) {
      var newNode = {
        key: treeNode.key,
        level: treeNode.level + 1,
        type: ROW_PERMISSION_RELATION.AND,
        children: [{
          rowValues: treeNode.rowValues,
          key: iup.uniqueId(),
          level: (treeNode === null || treeNode === void 0 ? void 0 : treeNode.level) + 1
        }, {
          rowValues: initRowValue,
          key: iup.uniqueId(),
          level: (treeNode === null || treeNode === void 0 ? void 0 : treeNode.level) + 1
        }]
      };
      delete treeNode.rowValues;
      Object.assign(treeNode, newNode);
      return;
    }
    for (var i = 0; i < children.length; i += 1) {
      if (children[i].key !== key) continue;
      if ((treeNode === null || treeNode === void 0 ? void 0 : treeNode.level) <= maxLevel) {
        children[i] = {
          key: children[i].key,
          type: ROW_PERMISSION_RELATION.AND,
          level: (treeNode === null || treeNode === void 0 ? void 0 : treeNode.level) + 1,
          children: [Object.assign({}, children[i], {
            key: iup.uniqueId(),
            level: (treeNode === null || treeNode === void 0 ? void 0 : treeNode.level) + 1
          }), Object.assign({
            key: iup.uniqueId(),
            rowValues: initRowValue,
            level: (treeNode === null || treeNode === void 0 ? void 0 : treeNode.level) + 1
          })]
        };
      }
    }
  };

  /**
   * 处理条件删除
   */
  var handleDeleteCondition = function handleDeleteCondition(key) {
    var cloneData = iup.deepCopyEx(value);
    var deleteNode = findRelationNode(cloneData, key, false);
    if (notEmpty.data && !(deleteNode !== null && deleteNode !== void 0 && deleteNode.children)) return message.info(notEmpty.message);
    if (!notEmpty.data && !(deleteNode !== null && deleteNode !== void 0 && deleteNode.children)) {
      return onChange === null || onChange === void 0 ? void 0 : onChange(undefined);
    }
    deleteCondition(deleteNode, key);
    onChange === null || onChange === void 0 || onChange(cloneData);
  };

  /**
   * 删除节点
   * - 删除当前节点下的一条数据或者是删除一个条件节点
   */
  var deleteCondition = function deleteCondition(parentData, key) {
    var _parentDataTemp, _parentDataTemp2;
    var parentDataTemp = parentData;
    parentDataTemp.children = (_parentDataTemp = parentDataTemp) === null || _parentDataTemp === void 0 || (_parentDataTemp = _parentDataTemp.children) === null || _parentDataTemp === void 0 ? void 0 : _parentDataTemp.filter(function (item) {
      return item.key !== key;
    });
    if (((_parentDataTemp2 = parentDataTemp) === null || _parentDataTemp2 === void 0 || (_parentDataTemp2 = _parentDataTemp2.children) === null || _parentDataTemp2 === void 0 ? void 0 : _parentDataTemp2.length) === 1) {
      var newChild = updateLevel(parentDataTemp.children[0]);
      var _key = parentDataTemp.key;
      delete parentDataTemp.children;
      delete parentDataTemp.type;
      parentDataTemp = Object.assign(parentDataTemp, _objectSpread(_objectSpread({}, newChild), {}, {
        key: _key,
        level: newChild.level
      }));
    }
  };

  /**
   * 删除一个条件节点时，更新当前数据的层级
   */
  var updateLevel = function updateLevel(node) {
    var newChildren;
    if (node.children) newChildren = node.children.map(function (element) {
      return updateLevel(element);
    });
    var newNode = _objectSpread(_objectSpread({}, node), {}, {
      children: newChildren,
      level: (node === null || node === void 0 ? void 0 : node.level) - 1
    });
    return newNode;
  };

  /**
   * 更改条件节点的条件
   */
  var handleChangeCondition = function handleChangeCondition(key, type) {
    var cloneData = iup.deepCopyEx(value);
    var changeNode = findRelationNode(cloneData, key, true);
    changeNode.type = type === ROW_PERMISSION_RELATION.AND ? ROW_PERMISSION_RELATION.OR : ROW_PERMISSION_RELATION.AND;
    onChange === null || onChange === void 0 || onChange(cloneData);
  };

  /**
   * 改变节点的的数据
   */
  var handleChangeRowValues = function handleChangeRowValues(key, values) {
    var _changeNode$rowValues;
    var cloneData = iup.deepCopyEx(value);
    var changeNode = findRelationNode(cloneData, key, true);
    changeNode.rowValues = _objectSpread(_objectSpread({}, (_changeNode$rowValues = changeNode.rowValues) !== null && _changeNode$rowValues !== void 0 ? _changeNode$rowValues : {}), values);
    onChange === null || onChange === void 0 || onChange(cloneData);
  };
  return /*#__PURE__*/_jsx(RulesController, {
    maxLevel: maxLevel,
    maxSize: maxSize,
    disabled: disabled,
    value: value,
    component: component,
    onAddCondition: handleAddCondition,
    onDeleteCondition: handleDeleteCondition,
    onChangeCondition: handleChangeCondition,
    onChangeRowValues: handleChangeRowValues
  });
};